package com.wnnight.wys.serviceImpl.payment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wnnight.wys.entity.PageBean;
import com.wnnight.wys.entity.config.ChargeConfig;
import com.wnnight.wys.entity.lanlord.Landlord;
import com.wnnight.wys.entity.payment.PayMentMethod;
import com.wnnight.wys.entity.payment.PayMentRecords;
import com.wnnight.wys.entity.room.Room;
import com.wnnight.wys.mapper.config.ChargeConfigMapper;
import com.wnnight.wys.mapper.landlord.LandlordMapper;
import com.wnnight.wys.mapper.payment.PayMentMapper;
import com.wnnight.wys.mapper.payment.PayMentMethodMapper;
import com.wnnight.wys.mapper.room.RoomMapper;
import com.wnnight.wys.service.payment.PayMentService;
import com.wnnight.wys.util.config.MyBatisUtil;
import com.wnnight.wys.vo.PayMentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: PayMentServiceImpl
 * @author: LJC
 * @date: 2022/8/29-10:53
 */
@Service
@Slf4j
public class PayMentServiceImpl extends ServiceImpl<PayMentMapper, PayMentRecords> implements PayMentService {
    @Autowired
    private PayMentMapper payMentMapper;
    @Autowired
    private ChargeConfigMapper configMapper;
    @Autowired
    private PayMentMethodMapper payMentMethodMapper;
    @Autowired
    private LandlordMapper landlordMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public void addPayMent(PayMentRecords pm) {
        //UUID
        //16位账单编号
        String uuid = UUID.randomUUID().toString();
        String rep = uuid.replace("-", "");
        String repuuid = rep.substring(0, 16);
        pm.setBillNumber(repuuid); //账单编号

        //获取当前系统时间
        String nowTime = MyBatisUtil.getNowTime();
        pm.setCollectionTime(nowTime);//应收时间
        log.debug(String.valueOf(pm));
//        Room room = new Room;
//        String s = String.valueOf(pm.getRoom().getId());
//        room.setId();
//        pm.setRoom();

        //增加缴费记录
        payMentMapper.insert(pm);
    }


    @Override
    public PageBean<List<PayMentRecords>> getAllPayMent(PageBean<PayMentRecords> page) {
        PageBean<List<PayMentRecords>> pageBean = new PageBean<>();
        //分页插件
        Page<PayMentRecords> p = PageHelper.startPage(page.getCurrPage(), page.getPageSize());
        List<PayMentRecords> allPayMR = payMentMapper.getAllPayMR();

        pageBean.setCurrPage(p.getPageNum()); // 获取当前页数
        pageBean.setPageSize(p.getPageSize()); //获取每页页数
        pageBean.setTotalPages(p.getPages()); //获取总页数
        pageBean.setTotalNum((int) p.getTotal()); //获取总数
        pageBean.setData(allPayMR);

        //
        return pageBean;
    }

    @Override
    public PageBean<List<PayMentRecords>> getPayMent(PayMentVo payMentVo) {
        PageBean<List<PayMentRecords>> pageBean = new PageBean<>();
        PageBean<PayMentRecords> page = payMentVo.getPageBean();
        //分页插件
        Page<PayMentRecords> p = PageHelper.startPage(page.getCurrPage(), page.getPageSize());
        List<PayMentRecords> allPayMR = payMentMapper.findPayMent(payMentVo);

        pageBean.setCurrPage(p.getPageNum()); // 获取当前页数
        pageBean.setPageSize(p.getPageSize()); //获取每页页数
        pageBean.setTotalPages(p.getPages()); //获取总页数
        pageBean.setTotalNum((int) p.getTotal()); //获取总数
        pageBean.setData(allPayMR);

        //
        return pageBean;
    }

    //根据业主id查询
    @Override
    public PageBean<List<PayMentRecords>> getByLandlordId(PayMentVo payMentVo) {
        PageBean<List<PayMentRecords>> pageBean = new PageBean<>();
        PageBean<PayMentRecords> page = payMentVo.getPageBean();
        //分页插件
        Page<PayMentRecords> p = PageHelper.startPage(page.getCurrPage(), page.getPageSize());
        List<PayMentRecords> allPayMR = payMentMapper.getByLandlordId(payMentVo.getPayMentRecords());

        pageBean.setCurrPage(p.getPageNum()); // 获取当前页数
        pageBean.setPageSize(p.getPageSize()); //获取每页页数
        pageBean.setTotalPages(p.getPages()); //获取总页数
        pageBean.setTotalNum((int) p.getTotal()); //获取总数
        pageBean.setData(allPayMR);

        //
        return pageBean;
    }
    //返回全部账目
    @Override
    public List<ChargeConfig> getConfigList() {
        QueryWrapper<ChargeConfig> wrapper = new QueryWrapper<>();
        wrapper.ne("status","n");
        List<ChargeConfig> list = new ArrayList<>();
        ChargeConfig c = new ChargeConfig();
        c.setId(0);
        c.setChargeProject("全部账目");
        list = configMapper.selectList(wrapper);
        //list.add(0,c);
        return list;
    }

    //返回支付方式
    @Override
    public List<PayMentMethod> getPayMentMethod() {
        QueryWrapper<PayMentMethod> wrapper = new QueryWrapper<>();
        List<PayMentMethod> list = new ArrayList<>();
        //PayMentMethod p = new PayMentMethod(0,"全部支付方式");
        list = payMentMethodMapper.selectList(wrapper);
        //list.add(0,p);
        return list;
    }

    //获取所有缴费列表
    @Override
    public List<PayMentRecords> getAllList() {
        return payMentMapper.getAllPayMR();
    }

    //返回业主列表
    @Override
    public List<Landlord> getPayLandlordList() {
        QueryWrapper<Landlord> wrapper = new QueryWrapper<>();
        wrapper.select("id","name");
        return landlordMapper.selectList(wrapper);
    }

    //返回房屋
    @Override
    public List<Map<String,Object>> getPayRoomList() {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.select("id","building_num","unit","floor","room_num");
        List<Room> allroom = roomMapper.selectList(wrapper);
        List<Map<String,Object>> list = new ArrayList<>();
        //list.forEach(System.out::println);
        for (Room room : allroom) {
            Map<String,Object> map = new HashMap<>();
            String p = room.getBuildingNum() + "-" + room.getUnit() + "-" + room.getFloor() + "楼 " + room.getRoomNum() + "号";
            map.put("id",String.valueOf(room.getId()));
            map.put("name",p);
            list.add(map);
        }
        return list;
    }
}
